package control

import (
	"fmt"
	"manager/model"
	"manager/mqtt/publish"
	"net/http"
	"strconv"

	"github.com/lerryxiao/gin"
	"hiscene.com/openplatform/common/retCode"
)

////////////////////////////////////////////////////////////////////////////////

func getLamp(c *gin.Context) *model.Lamp {
	sid := c.Request.FormValue("id")
	if len(sid) <= 0 {
		c.JSON(http.StatusOK, gin.H{"retCode": retCode.E_C_BAD_PARAM, "comment": "id为空"})
		return nil
	}

	id, err := strconv.ParseUint(sid, 10, 64)
	if id <= 0 || err != nil {
		c.JSON(http.StatusOK, gin.H{"retCode": retCode.E_C_BAD_PARAM, "comment": "id解析失败"})
		return nil
	}

	lamp := GetLampByID(id)
	if lamp == nil {
		c.JSON(http.StatusOK, gin.H{"retCode": retCode.E_C_BAD_STATUS, "comment": "获取灯失败"})
		return nil
	}

	return lamp
}

func getBoard(c *gin.Context) *model.Board {
	sid := c.Request.FormValue("id")
	if len(sid) <= 0 {
		c.JSON(http.StatusOK, gin.H{"retCode": retCode.E_C_BAD_PARAM, "comment": "id为空"})
		return nil
	}

	id, err := strconv.ParseUint(sid, 10, 64)
	if id <= 0 || err != nil {
		c.JSON(http.StatusOK, gin.H{"retCode": retCode.E_C_BAD_PARAM, "comment": "id解析失败"})
		return nil
	}

	board := GetBoardByID(id)
	if board == nil {
		c.JSON(http.StatusOK, gin.H{"retCode": retCode.E_C_BAD_STATUS, "comment": "获取板子失败"})
		return nil
	}

	return board
}

func getLampBoard(c *gin.Context) (*model.Lamp, *model.Board) {
	lamp := getLamp(c)
	if lamp == nil {
		return nil, nil
	}
	board := GetBoardByID(lamp.BoardID)
	if board == nil {
		c.JSON(http.StatusOK, gin.H{"retCode": retCode.E_C_BAD_STATUS, "comment": "获取板子失败"})
		return nil, nil
	}

	return lamp, board
}

////////////////////////////////////////////////////////////////////////////////

// lampList 灯列表
func lampList(c *gin.Context) {
	mac := c.Request.FormValue("mac")
	if len(mac) <= 0 {
		c.JSON(http.StatusOK, gin.H{"retCode": retCode.S_OK, "data": map[string]interface{}{
			"boards": boards,
			"lamps":  lamps,
		}})
	} else {
		tlamps := make(map[uint64]*model.Lamp)
		board := GetBoard(mac)
		if board != nil {
			for _, lamp := range lamps {
				if lamp != nil && lamp.BoardID == board.ID {
					tlamps[lamp.ID] = lamp
				}
			}
			c.JSON(http.StatusOK, gin.H{"retCode": retCode.S_OK, "data": map[string]interface{}{
				"boards": map[uint64]interface{}{
					board.ID: board,
				},
				"lamps": tlamps,
			}})
		} else {
			c.JSON(http.StatusOK, gin.H{"retCode": retCode.S_OK, "comment": "no data find"})
		}
	}
}

// lampOn 灯打开
func lampOn(c *gin.Context) {
	lamp, board := getLampBoard(c)
	if lamp == nil || board == nil {
		return
	}
	lamp.OnOff = true
	SaveLamp(lamp)

	publish.Send(board.Mac, "on", lamp.Key)

	c.JSON(http.StatusOK, gin.H{"retCode": retCode.S_OK, "comment": fmt.Sprintf("lamp %v on", lamp.Name)})
}

// lampOff 灯关闭
func lampOff(c *gin.Context) {
	lamp, board := getLampBoard(c)
	if lamp == nil || board == nil {
		return
	}
	lamp.OnOff = false
	SaveLamp(lamp)

	publish.Send(board.Mac, "off", lamp.Key)

	c.JSON(http.StatusOK, gin.H{"retCode": retCode.S_OK, "comment": fmt.Sprintf("lamp %v off", lamp.Name)})
}

// lampEdit 灯编辑
func lampEdit(c *gin.Context) {
	lamp := getLamp(c)
	if lamp == nil {
		return
	}

	bchange := false
	name := c.Request.FormValue("name")
	if len(name) > 0 && lamp.Name != name {
		lamp.Name = name
		bchange = true
	}

	disable := c.Request.FormValue("disable")
	if len(disable) > 0 {
		switch disable {
		case "false", "False", "0":
			{
				if lamp.Disable == true {
					lamp.Disable = false
					bchange = true
				}
			}
		case "true", "TRUE", "1":
			{
				if lamp.Disable == false {
					lamp.Disable = true
					bchange = true
				}
			}
		}
	}

	if bchange == true {
		SaveLamp(lamp)
		c.JSON(http.StatusOK, gin.H{"retCode": retCode.S_OK, "comment": "ok"})
	} else {
		c.JSON(http.StatusOK, gin.H{"retCode": retCode.S_OK, "comment": "no changed"})
	}
}
